package com.wwj.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.springboot.dao.ProductMapper;
import com.wwj.springboot.entity.Product;
import com.wwj.springboot.service.OrderService;
import com.wwj.springboot.service.ProductService;
import io.goeasy.GoEasy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wwj
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderService orderService;

    @Override
    public List<Product> selectAllProduct() {
        List<Product> selectAllProduct  = productMapper.selectAllProduct();
        return selectAllProduct;
    }

    @Override
    public Product selectOneProduct(int pid) {
        Product product = productMapper.selectById(pid);
        return product;
    }

    @Override
    public void updateProduct(int pid,int userid) {


        Product product = selectOneProduct(pid);

        GoEasy goEasy = new GoEasy( "rest-hangzhou.goeasy.io", "BC-45150c1639b04b0388a60611724f1005");
        if (product.getAccount()!=0 && product.getAccount()>0){

            try {
                productMapper.updateProduct(pid);
                orderService.addOrder(pid,userid);
                goEasy.publish("my_channel1", "抢购成功");
            } catch (Exception e) {
                e.printStackTrace();
                goEasy.publish("my_channel1", "抢购失败");
            }
        }else {
            goEasy.publish("my_channel1", "已经被抢购没了");
        }
    }
}
